/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.io;

import jigdo.utils.SimpleIniParser;
import jigdo.JigdoException;
import jigdo.data.jigdo.JigdoRemoteFileEntry;
import jigdo.data.jigdo.JigdoFile;
import com.jcraft.jzlib.GZIPInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Logger;

/**
 * Utility class to parse a .jigdo file from the disk.
 * @author John R Sohn
 * @see jigdo.data.jigdo.JigdoFile
 */
public class JigdoFileReader {

    
    public static void setParentLogger(Logger parent)
    {
        logger.setParent(parent);
        logger.setLevel(null);
    }
    
    static Logger logger = Logger.getLogger(JigdoFileReader.class.getName());
    /**
     * Reads the entire jigdo file from the disk and parses it.
     * @param filename the path and filenameof the .jigdo file
     * @return the parsed jigdo file 
     * @throws IOException
     * @throws JigdoException 
     */
    public static JigdoFile ReadFile(byte[] file) throws IOException, JigdoException {
        
        
        java.io.ByteArrayInputStream stream = new java.io.ByteArrayInputStream(file);
        
        GZIPInputStream gz;

        try {
            gz = new GZIPInputStream(stream);
        } catch (IOException e) {
            throw new IOException("Error while attempting to unzip Jigdo Archive", e);
        }

        SimpleIniParser sip = new SimpleIniParser(gz);

        gz.close();

        jigdo.data.jigdo.JigdoFile jf = new jigdo.data.jigdo.JigdoFile();

        jf.Filename = sip.GetOption("image", "Filename");
        jf.Template = sip.GetOption("image", "Template");
        jf.Template_MD5Sum = sip.GetOption("image", "Template-MD5Sum");
        jf.Generator = sip.GetOption("Jigdo", "Generator");
        jf.Info = sip.GetOption("Image", "Info");
        jf.ShortInfo = sip.GetOption("Image", "ShortInfo");
        jf.Version = sip.GetOption("Jigdo", "Version");

        jf.Files.clear();

        Iterator i = sip.GetSectionNoCase("Parts").Options.iterator();

        while (i.hasNext()) {
            
            SimpleIniParser.Option item = (SimpleIniParser.Option) i.next();
            JigdoRemoteFileEntry newj = new JigdoRemoteFileEntry(jf);
            newj.MD5 = item.Name;
            String[] values = item.Value.split(":");

            newj.URL = (values.length > 1) ? values[1] : ((values.length > 0) ? values[0] : "");
            newj.SERVER = (values.length > 1) ? values[0] : "";
            jf.Files.add(newj);
        }
        
       SimpleIniParser.IniSection serverSect =sip.GetSectionNoCase("Servers");
       
       for (int x=0; x < serverSect.Options.size(); x++)
       {
           SimpleIniParser.Option curr = serverSect.Options.get(x);
          jf.Servers.add(new jigdo.data.jigdo.JigdoServerEntry(curr.Name,curr.Value,jf));
       }
       
        return jf;
    }
}
